#if defined _td_tools_included
	#endinput
#endif

#define _td_tools_included



/*	This method gives item to player if he has enough money and decreases players money
	if td_can_give_item method returns false item will not be given
*/
stock td_give_item_from_dealer(id, item, dealer, bool:auto=false) {
	new sString[64];
	new itemName[64];
	td_get_item_name(id, item, itemName, id);
	
	if(gDealersItems[dealer][item][Dii_Count] >0){
		if(!td_can_give_item(id, item)){
			return false;
		}
		new cost = gDealersItems[dealer][item][Dii_Cost];
		if(auto){
			new type = get_pcvar_num(gCvarAutoBuyCostType);
			switch(type){
				case 1: cost+= get_pcvar_num(gCvarAutoBuyCostVal);
				case 2: cost+= cost*get_pcvar_float(gCvarAutoBuyCostVal);
			}
		}
		new money = cs_get_user_money(id);
		if(money >= cost){
			gDealersItems[dealer][item][Dii_Count] --;
			money-=cost;
			cs_set_user_money(id, money);
			
			td_item_bought(id, item);
			new costName[32];
			td_get_string(id, sCost, costName, charsmax(costName));
			client_print(id, print_chat, "%s %s: %s [%s:%d]", prefix_disp, sString, itemName, costName, cost);
			return true;
		} else {
			if(!auto){
				td_get_string(id, sNotEnoughMoney, sString, charsmax(sString));
				client_print(id, print_chat, "%s %s", prefix_disp, sString);
			}
		}
	} else {
		if(!auto) {
			td_get_string(id, sItemsSold, sString, charsmax(sString));
			client_print(id, print_chat, "%s %s %s (%s)", prefix_disp, gDealerDef[dealer][Di_Name], sString, itemName);
		}
	}
	return false;
}

stock td_quick_buy_item(id, item){
	
	for(new dealer = 0; dealer<MAX_DEALERS; dealer++){
		if(td_give_item_from_dealer(id, item, dealer, true))
			return;
	}
	new sString[64];
	td_get_string(id, sItemsSold, sString, charsmax(sString);
	client_print(id, print_chat, "%s %s", prefix_disp,  sString);
}

/*	This method return count of all items dealer has
*/
stock td_get_dealers_items_count(dealer){
	new count =0 ;
	for(new item=0;item<Items;item++)
		count += gDealersItems[dealer][item][Dii_Count];
	return count;
}

/*	This method returns true if any dealer (available to player) has item
*/
stock bool:td_any_dealer_have_item(id, item){
	for(new dealer=0;dealer<MAX_DEALERS;dealer++){
		if(!gDealerDef[dealer][Di_Registered])
			continue;
		if(playerAvailableDealers[id][dealer] && gDealersItems[dealer][item][Dii_Count] >0)
			return true;
	}
	return false;
}
